package org.springframework.boot.netty.handler.factory;

import io.netty.channel.ChannelHandler;
import org.apache.commons.lang3.ClassUtils;
import org.springframework.beans.BeansException;
import org.springframework.boot.netty.exception.ChannelHandlerException;
import org.springframework.boot.netty.handler.HeartBeatHandler;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;

public class HearBeatHandlerFactory implements NettyCustomerizerChannelInboundHandlerAdapterFactory<HeartBeatHandler> {


    private ApplicationContext applicationContext;

    @Override
    public Class<?> getObjectType() {
        return HeartBeatHandler.class;
    }

    @Override
    public ChannelHandler getObject() {
        try {
            HeartBeatHandler heartBeatHandler = (HeartBeatHandler) getObjectType().newInstance();
            heartBeatHandler.setApplicationContext(this.applicationContext);
            return heartBeatHandler;
        } catch (ReflectiveOperationException e){
            throw new ChannelHandlerException(ClassUtils.getName(this)+" can not create channelHandler");
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        this.applicationContext = applicationContext;
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {

    }

    @Override
    public int getOrder() {
        return this.LOWEST_PRECEDENCE;
    }
}
